<template>
	<view class="pageview">
		<cl-navbar :title="$t('addressbook.index.title')" autoBack placeholder @rightClick="addFriend">
			<template slot="right">
				<image src="/static/icon/addfriend.svg" style="width: 40rpx;height: 40rpx;"></image>
			</template>
		</cl-navbar>
		<view class="searchview">
			<u-search height="64rpx" :showAction="false" :clearabled="false" bgColor="#1e1f30" :inputStyle="{fontSize:'24rpx'}" :placeholder="$t('commonality.search')" @input="searchInput"></u-search>
		</view>
		<view class="listview">
			<u-index-list :index-list="newData.indexList" :customNavHeight="98 + $u.sys().statusBarHeight">
				<view slot="header" class="cellviewGrop">
					<view class="cellview" @click="gopage('friend_apply')">
						<view class="cellview__left">
							<image class="cellview__left__img"
								src="/static/icon/newfriend.svg"
								mode=""></image>
							<text class="cellview__left__title">{{$t("addressbook.friend_apply.title")}}</text>
						</view>
						<view>
							<u-badge max="99" :value="newFriendCount"></u-badge>
						</view>
						
					</view>
					<view class="cellview"  @click="gopage('group_create')">
						<view class="cellview__left">
							<image class="cellview__left__img"
								src="/static/icon/faqigroup.svg"
								mode=""></image>
							<text class="cellview__left__title">{{$t("group.group_create.title")}}</text>
						</view>
					</view>
					<view class="cellview" @click="gopage('friend_group')">
						<view class="cellview__left">
							<image class="cellview__left__img"
								src="/static/icon/myfriendgroup.svg"
								mode=""></image>
							<text class="cellview__left__title">{{$t("addressbook.index.myGroup")}}</text>
						</view>
					</view>
					<view class="cellview"  @click="gopage('group_list')">
						<view class="cellview__left">
							<image class="cellview__left__img"
								src="/static/icon/mygroup.svg"
								mode=""></image>
							<text class="cellview__left__title">{{$t("group.group_list.title")}}</text>
						</view>
					</view>
					<view class="cellview" @click="gopage('friend_black')">
						<view class="cellview__left">
							<image class="cellview__left__img"
								src="/static/icon/heimingdan.svg"
								mode=""></image>
							<text class="cellview__left__title">{{$t("addressbook.friend_black.title")}}</text>
						</view>
					</view>
				</view>
				<template v-for="(item, idx) in newData.itemArr">
					<u-index-item :key="idx">
						<u-index-anchor :text="newData.indexList[idx]"></u-index-anchor>
						<view class="listitemview">
							<view class="listitem" v-for="(cell, index) in item" :key="'item'+index" @click="goFriend(cell)">
								<view class="listitem__left">
									<cl-avatar :src="cell.avatar_url" size="100rpx"></cl-avatar>
								</view>
								<view class="listitem__right">
									<view class="listitem__right__top">
										<text class="listitem__right__top__nickname">{{cell.friend_remark || cell.nick_name}}</text>
									</view>
									<view class="listitem__right__content">
										<text v-if="friendOnline[cell.friend_idx]" style="color: #6aaf76;font-size: 24rpx;">[{{$t("common.friend.online")}}]</text>
										<text v-else style="color: #595A66;font-size: 24rpx;">[{{$t("common.friend.offline")}}]</text>
									</view>
								</view>
							</view>
						</view>
					</u-index-item>
				</template>
			</u-index-list>
		</view>
	</view>
</template>

<script>
	import {
		conversion
	} from '@/common/tools/letterConversion.js'
	import {
		mapState
	} from 'vuex'
	export default {
		name: 'addressbook',
		data() {
			return {
				itemArrs: [],
				searchval:'',
			}
		},
		computed: {
			...mapState({
				friendList: state => state.friends.friendList,
				newFriendCount: state => state.tabbarbadge.newFriendCount,
				friendOnline: state => state.friends.friendOnline,
			}),
			newData(){
				console.log(this.friendList);
				if(this.friendList.length){
					let newdata = this.friendList.filter(item => {
						// 根据名字筛选
						if (this.searchval && item.friend_remark.indexOf(this.searchval) === -1 && item.nick_name.indexOf(this.searchval) === -1) {
							return false;
						}
						return true;
					});
					return this.conversion(newdata, 'friend_remark','nick_name')
				}
				return {
					indexList: [],
					itemArr: []
				}
			}
		},
		methods: {
			conversion,
			searchInput(e){
				uni.$u.debounce(()=>{
					this.searchval = e
				}, 500,false)
			},
			addFriend() {
				this.$Router.push({
					name: 'add_friend'
				});
			},
			gopage(name){
				this.$Router.push({
					name
				});
			},
			goFriend(item){
				this.$Router.push({
					name: 'friend_details',
					params: item
				});
				console.log(item.friend_idx);
			}
		},
	};
</script>

<style scoped lang="scss">
	.list-cell {
		padding: 10px 24rpx;
		overflow: hidden;
		color: #fff;
		font-size: 14px;
		line-height: 24px;
		background-color: #121325;
	}

	.searchview {
		margin: 20rpx 30rpx;
		background-color: #1e1f30;
		border-radius: 60rpx;
		padding-right: 20rpx;
	}

	.cellviewGrop {
		background: #1E1F30;
		border-radius: 20rpx;
		margin: 20rpx 30rpx 30rpx;

		.cellview {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx;

			&__left {
				display: flex;
				align-items: center;

				&__img {
					width: 70rpx;
					height: 70rpx;
					border-radius: 20rpx;
				}

				&__title {
					font-size: 28rpx;
					margin-left: 20rpx;
					color: #FFFFFF;
				}
			}
		}
	}

	.listitemview {
		// margin-top: 30rpx;
		.listitem {
			display: flex;
			align-items: center;
			padding: 12rpx 30rpx;
			position: relative;

			&.active {
				background-color: #1E1F30;
			}

			// &::after {
			// 	content: "";
			// 	position: absolute;
			// 	width: calc(100% - 140rpx);
			// 	background-color: #1E1F30;
			// 	height: 1rpx;
			// 	bottom: 0;
			// 	right: 0;
			// }

			&__right {
				margin-left: 20rpx;
				flex: 1;

				&__top {
					display: flex;
					align-items: center;

					&__nickname {
						color: #fff;
						font-size: 28rpx;
					}

					&__time {
						color: #61626E;
						font-size: 20rpx;
					}
				}

				// &__content {
				// 	display: flex;
				// 	justify-content: space-between;
				// 	align-items: center;
				// 	margin-top: 15rpx;

				// 	&__msg {
				// 		flex: 1;
				// 		font-size: 24rpx;
				// 		color: #595A66;
				// 	}
				// }
			}
		}
	}
</style>